package entity;

import java.text.SimpleDateFormat;
import java.util.Date;
public class Book implements Comparable<Book> {
   public Book() {}
   public Book(int id, String name, double price, String author, Date date) {
      this.id = id;
      this.name = name;
      this.price = price;
      this.author = author;
      this.date = date;
   }
   private int id; //id编号
   private String name; //名字
   private double price; //价格
   private String author; //作者
   private Date date; //日期

   public int getId() {
      return id;
   }

   public void setId(int id) {
      this.id = id;
   }

   public String getName() {
      return name;
   }

   public void setName(String name) {
      this.name = name;
   }

   public double getPrice() {
      return price;
   }

   public void setPrice(double price) {
      this.price = price;
   }

   public String getAuthor() {
      return author;
   }

   public void setAuthor(String author) {
      this.author = author;
   }

   public Date getDate() {
      return date;
   }

   public void setDate(Date date) {
      this.date = date;
   }


   @Override
   public String toString() {
      SimpleDateFormat dateFor=new SimpleDateFormat("yyyy-MM-dd");

      return "entity.Book{" +
              "id=" + id +
              ", name='" + name + '\'' +
              ", price=" + price +
              ", author='" + author + '\'' +
              ", date=" + dateFor.format(date) +
              '}';
   }

   @Override
   public int compareTo(Book o) {
      /*内含 两个参数 本身是this 外部是o*/
      //如果我们按照价格排序 则可以写为
      if(this.price>o.price){
         return -1;//降序
       }else if(this.price<o.price){
         return 1;//**
      }

      //价格相同时 按照id升序排序
      if(this.id>o.id){
         return 1;
      } else if (this.id<o.id) {
         return -1;
      }
      return 0;//相等
   }
}
